﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;

    public class PCRequest : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnSave;
        protected CheckBox chbOnlyNonEmptyLoc;
        protected CheckBox chbShowSystemQty;
        protected DropDownList ddlStatus;
        protected HtmlForm F5541;
        protected GridView gvPcMainTenanceDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPrID;
        protected HtmlInputHidden hdnTagNo;
        protected HtmlInputButton htmlbtnReport;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage img3;
        protected HtmlImage img4;
        protected HtmlImage img5;
        protected HtmlImage imgOwnerID;
        protected HtmlImage imgPutawayLoc;
        protected HtmlImage imgZoneID;
        protected Label lblAreaIDEnd;
        protected Label lblAreaIDStart;
        protected Label lblDescr;
        protected Label lblEnd;
        protected Label lblEndingTagNo;
        protected Label lblLocEnd;
        protected Label lblLocLevelEnd;
        protected Label lblLocLevelStart;
        protected Label lblLocStart;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerEnd;
        protected Label lblOwnerStart;
        protected Label lblPhysicalRequestID;
        protected Label lblPlanEndTime;
        protected Label lblPlanStartTime;
        protected Label lblSkuEnd;
        protected Label lblSkuStart;
        protected Label lblStart;
        protected Label lblStartingTagNo;
        protected Label lblStatus;
        protected Label lblZoneIDEnd;
        protected Label lblZoneIDStart;
        protected LinkButton lnkbtnPcDetele;
        protected LinkButton lnkbtnPcHold;
        protected LinkButton lnkbtnPcOK;
        protected LinkButton lnkbtnPcRequestTasks;
        protected LinkButton lnkbtnPrintPCMaintenance;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnPcDetele;
        protected HtmlTableCell tdlnkbtnPcHold;
        protected HtmlTableCell tdlnkbtnPcOK;
        protected HtmlTableCell tdlnkbtnPcRequestTasks;
        protected HtmlTableCell tdlnkbtnPrintPCMaintenance;
        protected TextBox txtAreaIDEnd;
        protected TextBox txtAreaIDStart;
        protected TextBox txtDescr;
        protected TextBox txtEndingTagNo;
        protected TextBox txtLocEnd;
        protected TextBox txtLocLevelEnd;
        protected TextBox txtLocLevelStart;
        protected TextBox txtLocStart;
        protected TextBox txtOwnerEnd;
        protected TextBox txtOwnerStart;
        protected TextBox txtPhysicalRequestID;
        protected TextBox txtPlanEndTime;
        protected TextBox txtPlanStartTime;
        protected TextBox txtSkuEnd;
        protected TextBox txtSkuStart;
        protected TextBox txtStartingTagNo;
        protected TextBox txtZoneIDEnd;
        protected TextBox txtZoneIDStart;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_002");
                }
                else
                {
                    List<string> DeleteID = new List<string> {
                        this.txtPhysicalRequestID.Text
                    };
                    this.lblMsg.Text = StockCountServiceClientProxy.GetInstance().DeletePhysicalRequest(DeleteID) + base.GetGlobalMsgResource("C006");
                    this.hdnFlag.Value = "";
                    this.txtPhysicalRequestID.Enabled = true;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            if (this.hdnFrom.Value == "list")
            {
                base.Response.Redirect("PCRequestList.aspx?from=edit", false);
            }
            else
            {
                base.Response.Redirect("PCRequestQuery.aspx", false);
            }
        }

        protected void btnPcHold_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5760_008");
            }
            else
            {
                string strID = string.Empty;
                if (!string.IsNullOrEmpty(this.txtPhysicalRequestID.Text) && (this.hdnFlag.Value == "1"))
                {
                    strID = StockCountServiceClientProxy.GetInstance().InvHoldForPC(this.txtPhysicalRequestID.Text, "HOLD");
                    if (string.IsNullOrEmpty(strID))
                    {
                        this.lblMsg.Text = this.txtPhysicalRequestID.Text + base.GetGlobalMsgResource("F5760_009");
                    }
                    else
                    {
                        this.lblMsg.Text = strID;
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_010");
                }
            }
        }

        protected void btnPcOK_Clikc(object sender, EventArgs e)
        {
            if ((this.ddlStatus.SelectedValue.ToUpper() == "RECOUNTED") || (this.ddlStatus.SelectedValue.ToUpper() == "CLOSED"))
            {
                string strID = string.Empty;
                if (!string.IsNullOrEmpty(this.txtPhysicalRequestID.Text) && (this.hdnFlag.Value == "1"))
                {
                    strID = StockCountServiceClientProxy.GetInstance().InvHoldForPC(this.txtPhysicalRequestID.Text, "OK");
                    if (string.IsNullOrEmpty(strID))
                    {
                        this.lblMsg.Text = this.txtPhysicalRequestID.Text + base.GetGlobalMsgResource("F5760_011");
                    }
                    else
                    {
                        this.lblMsg.Text = strID;
                    }
                    this.lblMsg.Visible = true;
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_012");
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5760_013");
            }
        }

        protected void btnPcRequestTasks_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5760_005");
                this.lblMsg.Visible = true;
            }
            else if (!string.IsNullOrEmpty(this.txtPhysicalRequestID.Text))
            {
                if (!string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ReleasePcTasks(new List<string> { this.txtPhysicalRequestID.Text }, base.UserInfo.UserID, "CReleased")))
                {
                    this.SetPhysicalRequestInfo2Form();
                    this.lblMsg.Text = this.txtPhysicalRequestID.Text + base.GetGlobalMsgResource("F5760_006");
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5760_007");
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtPlanStartTime.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_003");
                }
                else if (string.IsNullOrEmpty(this.txtPlanEndTime.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_004");
                }
                else if (string.IsNullOrEmpty(this.hdnFlag.Value))
                {
                    this.InsertPhysicalRequest();
                }
                else
                {
                    this.UpdatePhysicalRequest();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void gvPcMainTenanceDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5541", this.gvPcMainTenanceDetail);
        }

        protected void gvPcMainTenanceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as PhysicalRequestInfo).PhysicalTagInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvPcMainTenanceDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
            }
        }

        private void InsertPhysicalRequest()
        {
            try
            {
                string PrID = string.Empty;
                PhysicalRequestInfo physicalRequestInfo = new PhysicalRequestInfo {
                    PhysicalRequestID = this.txtPhysicalRequestID.Text.Trim(),
                    Status = this.ddlStatus.SelectedValue,
                    Descr = this.txtDescr.Text
                };
                if (!string.IsNullOrEmpty(this.txtStartingTagNo.Text))
                {
                    physicalRequestInfo.StartingTagNo = Convert.ToInt32(this.txtStartingTagNo.Text);
                }
                physicalRequestInfo.PlanStartTime = Convert.ToDateTime(this.txtPlanStartTime.Text);
                physicalRequestInfo.PlanEndTime = Convert.ToDateTime(this.txtPlanEndTime.Text);
                physicalRequestInfo.OwnerIdStart = this.txtOwnerStart.Text;
                physicalRequestInfo.OwnerIdEnd = this.txtOwnerEnd.Text;
                physicalRequestInfo.SkuIdStart = this.txtSkuStart.Text;
                physicalRequestInfo.SkuIdEnd = this.txtSkuEnd.Text;
                physicalRequestInfo.LocStart = this.txtLocStart.Text;
                physicalRequestInfo.LocEnd = this.txtLocEnd.Text;
                if (!string.IsNullOrEmpty(this.txtLocLevelStart.Text))
                {
                    physicalRequestInfo.LocLevelStart = Convert.ToInt32(this.txtLocLevelStart.Text);
                }
                if (!string.IsNullOrEmpty(this.txtLocLevelEnd.Text))
                {
                    physicalRequestInfo.LocLevelEnd = Convert.ToInt32(this.txtLocLevelEnd.Text);
                }
                physicalRequestInfo.ZoneIdStart = this.txtZoneIDStart.Text;
                physicalRequestInfo.ZoneIdEnd = this.txtZoneIDEnd.Text;
                physicalRequestInfo.AreaIdStart = this.txtAreaIDStart.Text;
                physicalRequestInfo.AreaIdEnd = this.txtAreaIDEnd.Text;
                physicalRequestInfo.ShowSystemQty = this.chbShowSystemQty.Checked ? "Y" : "N";
                physicalRequestInfo.OnlyNonEmptyLoc = this.chbShowSystemQty.Checked ? "Y" : "N";
                physicalRequestInfo.CreatedBy = base.UserInfo.UserID;
                PrID = StockCountServiceClientProxy.GetInstance().InsertPhysicalRequest(physicalRequestInfo);
                if (!string.IsNullOrEmpty(PrID))
                {
                    this.hdnPrID.Value = PrID;
                    this.lblMsg.Text = PrID + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtPhysicalRequestID.Text = PrID;
                    this.txtPhysicalRequestID.Enabled = false;
                    this.hdnTagNo.Value = "10";
                    this.hdnFlag.Value = "1";
                    base.DataEntityViewState = physicalRequestInfo;
                    this.SetPhysicalRequestInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "PCStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnPrID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetPhysicalRequestInfo2Form();
            }
            else
            {
                this.ddlStatus.SelectedValue = "New";
                this.ddlStatus.Enabled = false;
                this.txtStartingTagNo.Text = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["detail"].ToString() + ",2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPhysicalRequestInfo2Form()
        {
            try
            {
                string PhysicalRequestID = this.hdnPrID.Value.ToString();
                PhysicalRequestInfo physicalRequestInfo = StockCountServiceClientProxy.GetInstance().GetPhysicalRequestByID(PhysicalRequestID);
                base.DataEntityViewState = physicalRequestInfo;
                if (physicalRequestInfo != null)
                {
                    this.txtPhysicalRequestID.Text = physicalRequestInfo.PhysicalRequestID;
                    this.txtPhysicalRequestID.Enabled = false;
                    this.ddlStatus.SelectedValue = physicalRequestInfo.Status;
                    this.ddlStatus.Enabled = false;
                    this.txtDescr.Text = physicalRequestInfo.Descr;
                    this.txtStartingTagNo.Text = physicalRequestInfo.StartingTagNo.ToString();
                    this.txtEndingTagNo.Text = "";
                    this.txtPlanStartTime.Text = base.GetPreferenceShortDate(physicalRequestInfo.PlanStartTime);
                    this.txtPlanEndTime.Text = base.GetPreferenceShortDate(physicalRequestInfo.PlanEndTime);
                    this.txtOwnerStart.Text = physicalRequestInfo.OwnerIdStart;
                    this.txtOwnerEnd.Text = physicalRequestInfo.OwnerIdEnd;
                    this.txtSkuStart.Text = physicalRequestInfo.SkuIdStart;
                    this.txtSkuEnd.Text = physicalRequestInfo.SkuIdEnd;
                    this.txtLocStart.Text = physicalRequestInfo.LocStart;
                    this.txtLocEnd.Text = physicalRequestInfo.LocEnd;
                    this.txtLocLevelStart.Text = physicalRequestInfo.LocLevelStart.ToString();
                    this.txtLocLevelEnd.Text = physicalRequestInfo.LocLevelEnd.ToString();
                    this.txtZoneIDStart.Text = physicalRequestInfo.ZoneIdStart;
                    this.txtZoneIDEnd.Text = physicalRequestInfo.ZoneIdEnd;
                    this.txtAreaIDStart.Text = physicalRequestInfo.AreaIdStart;
                    this.txtAreaIDEnd.Text = physicalRequestInfo.AreaIdEnd;
                    this.chbShowSystemQty.Checked = physicalRequestInfo.ShowSystemQty != "N";
                    this.chbOnlyNonEmptyLoc.Checked = physicalRequestInfo.OnlyNonEmptyLoc != "N";
                    base.BindData2GridView(this.gvPcMainTenanceDetail, physicalRequestInfo.PhysicalTagInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdatePhysicalRequest()
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5760_001");
                }
                else
                {
                    string PrID = string.Empty;
                    PhysicalRequestInfo physicalRequestInfo = base.DataEntityViewState as PhysicalRequestInfo;
                    physicalRequestInfo.Descr = this.txtDescr.Text;
                    physicalRequestInfo.StartingTagNo = Convert.ToInt32(this.txtStartingTagNo.Text);
                    physicalRequestInfo.PlanStartTime = Convert.ToDateTime(this.txtPlanStartTime.Text);
                    physicalRequestInfo.PlanEndTime = Convert.ToDateTime(this.txtPlanEndTime.Text);
                    physicalRequestInfo.OwnerIdStart = this.txtOwnerStart.Text;
                    physicalRequestInfo.OwnerIdEnd = this.txtOwnerEnd.Text;
                    physicalRequestInfo.SkuIdStart = this.txtSkuStart.Text;
                    physicalRequestInfo.SkuIdEnd = this.txtSkuEnd.Text;
                    physicalRequestInfo.LocStart = this.txtLocStart.Text;
                    physicalRequestInfo.LocEnd = this.txtLocEnd.Text;
                    if (!string.IsNullOrEmpty(this.txtLocLevelStart.Text))
                    {
                        physicalRequestInfo.LocLevelStart = Convert.ToInt32(this.txtLocLevelStart.Text);
                    }
                    if (!string.IsNullOrEmpty(this.txtLocLevelEnd.Text))
                    {
                        physicalRequestInfo.LocLevelEnd = Convert.ToInt32(this.txtLocLevelEnd.Text);
                    }
                    physicalRequestInfo.ZoneIdStart = this.txtZoneIDStart.Text;
                    physicalRequestInfo.ZoneIdEnd = this.txtZoneIDEnd.Text;
                    physicalRequestInfo.AreaIdStart = this.txtAreaIDStart.Text;
                    physicalRequestInfo.AreaIdEnd = this.txtAreaIDEnd.Text;
                    physicalRequestInfo.ShowSystemQty = this.chbShowSystemQty.Checked ? "Y" : "N";
                    physicalRequestInfo.OnlyNonEmptyLoc = this.chbOnlyNonEmptyLoc.Checked ? "Y" : "N";
                    physicalRequestInfo.UpdatedBy = base.UserInfo.UserID;
                    PrID = StockCountServiceClientProxy.GetInstance().UpdatePhysicalRequest(physicalRequestInfo);
                    if (!string.IsNullOrEmpty(PrID))
                    {
                        this.lblMsg.Text = PrID + base.GetGlobalMsgResource("C002");
                        this.lblMsg.Visible = true;
                        base.DataEntityViewState = physicalRequestInfo;
                        this.SetPhysicalRequestInfo2Form();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

